package mystia.lorelei.app.entity.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import mystia.lorelei.app.entity.Purchase;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class PurchaseDTO {
	private Integer id;
	private Integer memberId;
	private List<PurchaseItem> purchaseItems;
	private BigDecimal totalPrice;
	private Integer score;
	private Timestamp time;
	
	
	public PurchaseDTO(Integer id, Integer memberId, List<PurchaseItem> purchaseItems, Integer score,Timestamp time) {
		this.id = id;
		this.memberId = memberId;
		this.purchaseItems = purchaseItems;
		this.score = score;
		this.time =time;
	}
	
	public PurchaseDTO(Purchase purchase) {
		this.id = purchase.getId();
		this.memberId = purchase.getMemberId();
		this.totalPrice = purchase.getTotalPrice();
		this.score = purchase.getScore();
		this.purchaseItems = Arrays.stream(purchase.getContent().split(", ")).map(item ->{
			String[] temp = item.split("-");
			return
				new PurchaseItem(Integer.parseInt(temp[0]),temp[1],Integer.parseInt(temp[2]),Double.parseDouble(temp[3]));
		}).collect(Collectors.toList());
		this.time = purchase.getTime();
	}
}

